/*
 * This program is a multiple Threads program
 * It model a competition between rabbit and tortoise
 * Author:charles lai
 * Date:2008-10-08 
 */
package charles.testproject.rabbitandtortoise;

import charles.testproject.test.Test;

public class RabbitVSTortoise implements Test{

	public void test() {
		Rabbit rabbit = new Rabbit();
		Tortoise tortoise = new Tortoise();
		Thread t1 = new Thread(rabbit);
		Thread t2 = new Thread(tortoise);
		t1.start();
		t2.start();
	}

}

class Rabbit implements Runnable {
	
	int goal = 100;
	int run =0;
	
	public void run() {
		while(true) {
			try {
				run += 10;
				System.out.println("Rabbit run for " + run);
				if(run >= goal) {
					System.out.println("Rabbit won!");
					return;
				}
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}

class Tortoise implements Runnable {

	int goal = 100;
	int run =0;
	
	public void run() {
		while(true) {
			try {
				run += 5;
				System.out.println("Tortoise run for " + run);
				if(run >= goal) {
					System.out.println("torroise won!");
					return;
				}
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}